<?php

if ('cli' !== php_sapi_name()) {
	exit('服务只能运行在cli sapi模式下' . PHP_EOL);
}

if (extension_loaded('swoole')) {
	define('IS_SWOOLE', 1);
}else{
	define('IS_SWOOLE', 0);
}

// 定义系统常量
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', __DIR__ . DS);
// 载入系统函数库
require_once ROOT . "System" . DS . "Library" . DS . "Function.php";

if (is_file(ROOT . 'vendor' . DS . 'autoload.php')) {
	require_once ROOT . 'vendor' . DS . 'autoload.php';
} else {
	// 自定义autoload方法
	function autoload($class)
	{
		$includePath = str_replace('\\', DS, $class);
		$targetFile = ROOT . $includePath . '.php';
		require_once($targetFile);
	}
	spl_autoload_register('autoload');
}


// 继承Core父类
class Gmu extends System\Core
{

	// 拉起worker进程前需要做的初始化工作
	public function initWorker()
	{ }

	// 拉起tasker进程前需要做的初始化工作
	// 比如初始化数据库类库
	// 比如初始化其他类库
	public function initTasker(\swoole_server $server, $workerId)
	{
		$di = \System\Component\Di::getInstance();
	}

	// 具体业务逻辑
	public function process($server, $param)
	{

		// 将param抛给model中的method，并获得到处理完后的数据
		$targetModel = '\Application\\Controller\\' . ucfirst($param['param']['model']);
		$targetModel = new $targetModel;
		$targetConfig['param'] = $param['param']['param'];
		$sendData = call_user_func_array(array($targetModel, $param['param']['method']), array($targetConfig));
		return $sendData;
		/*$action = $param['param']['method'];//'/index/index/user';
		$site = 'User';
		$param = isset($param['param']['param']) ? $param['param']['param'] : [];
		static $import = true;
		if ($import == true) {
		define('APP_PATH', "/home/wwwroot/swoole/{$site}/application"); //框架目录
		define('RPC_RUN', true);
		}
		$import = false;
		$_REQUEST['argv_rpc'] = $action; //index/index/index
		$path = "/home/wwwroot/swoole/{$site}/thinkphp/base.php"; //base文件所在路径
		require_once $path;
		$_GET = $_POST = $param;
		$app = new \think\App();
		$data = $app->run();
		return $data;*/
	}
}

function webHttp($param)
{
	// 将param抛给model中的method，并获得到处理完后的数据
	$targetModel = '\Application\\Controller\\' . ucfirst($param['param']['model']);
	$targetModel = new $targetModel;
	$targetConfig['param'] = $param['param']['param'];
	$sendData = call_user_func_array(array($targetModel, $param['param']['method']), array($targetConfig));
	return $sendData;
}

//如果没有安装swoole就用原始的方式就行处理
if (IS_SWOOLE==0) {
	$param = array(
		'type' => 'SW',
		'requestId' => time(),
		'param' => array(
			'model' => 'Account',
			'method' => 'cache_test',
			'param' => array(
				'username' => 'wahah',
				'password' => 'wahah',
			),
		)
	);
	webHttp($param);
}

//如果有安装swoole，则走下面的流程
if (IS_SWOOLE==1) {
	$gmu = new Gmu;
	// 开启一些配置项
	$gmu->initSetting(array(
		'http' => array(
			'host' => '0.0.0.0', //ip
			'port' => 6666,
		),
		'tcp' => array(
			'port' => 9081, //tcp 端口
		),
		'custom' => array(
			'tcpPack' => 'eof',    // 1.eof，eof拆包 2.length，length拆包
		),
		// 服务注册
		'serviceRegisterSetting' => array(
			'host' => '127.0.0.1',
			'port' => 6379,
			'serviceName' => 'account-service',
		),
	));
	$gmu->run();
}
